package com.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.beans.NewProject;
import com.beans.page.PageList;
import com.data.mapper.BaseMapper;
import com.data.mapper.NewProjectMapper;
import com.services.NewProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiangwei
 * @since 18/1/23
 */
@Service
public class NewProjectServiceImpl extends BaseServiceImpl<NewProject> implements NewProjectService {
    @Autowired
    private NewProjectMapper newProjectMapper;

    @Override
    public BaseMapper<NewProject> getMapper(){return newProjectMapper; }

    /**
     * 分页查询
     *
     * @param pageList
     * @param bean
     * @return
     */
    public PageList<NewProject> queryPageByNewProjece(PageList<NewProject> pageList, NewProject bean) {
        Page<NewProject> page;
        int pageStart;
        if (pageList.getiDisplayLength() == -1) {//不分页
            pageStart = 1;
            page = PageHelper.startPage(pageStart, 10000, false);
        } else {
            pageStart = pageList.getiDisplayStart() / pageList.getiDisplayLength() + 1;
            page = PageHelper.startPage(pageStart, pageList.getiDisplayLength());
        }
        getMapper().queryList(bean);
        pageList.setiTotalRecords(page.getTotal());
        pageList.setiTotalDisplayRecords(pageList.getiTotalRecords());

        List<NewProject> newProjects = new ArrayList<NewProject>();

        for(NewProject nP : page){
            nP.setTotalOfArea(nP.getAreaBottom().intValue()+"-"+nP.getAreaTop().intValue());
            nP.setTotalPrice(nP.getPriceBottom().intValue()+"-"+nP.getPriceTop().intValue());

            newProjects.add(nP);
        }

        pageList.setAaData(newProjects);

        return pageList;
    }

    public List<NewProject> queryExists(NewProject newProject){
        return newProjectMapper.queryExists(newProject);
    }

}
